package com.bdqn.servlet;


import com.bdqn.pojo.User;
import com.bdqn.service.StudentService;
import com.bdqn.service.StudentServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class StudentServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    StudentService userService = new StudentServiceImpl();

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String path = request.getParameter("path");
        if(path.equals("index")){
            index(request,response);
        }
    }

    public void index(HttpServletRequest request, HttpServletResponse response){

        List<User> users = userService.findUser();
        response.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();
        PrintWriter out = null;
        try {
            out=response.getWriter();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.write(gson.toJson(users));
        out.flush();
        out.close();
    }

    public void init() throws ServletException {
        // Put your code here
    }
}
